/*
 * Copyright (C) 2007  Vianney le Clément
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fsahoraires.gui.pages;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public abstract class AbstractPage implements Page {

	private String title, image;

	public AbstractPage(String title, String image) {
		this.title = title;
		this.image = image;
	}

	public String getTitle() {
		return title;
	}

	public String getImage() {
		return image;
	}

	public void update() {
	}

	public Control createControl(Composite parent) {
		Action[] actions = getActions();
		if (actions == null || actions.length == 0) {
			return createContent(parent);
		} else {
			final Composite comp = new Composite(parent, SWT.NONE);
			comp.setBackground(parent.getDisplay().getSystemColor(
					SWT.COLOR_LIST_SELECTION));
			comp.setBackgroundMode(SWT.INHERIT_FORCE);
			Button[] buttons = new Button[actions.length];
			for (int i = 0; i < actions.length; i++) {
				buttons[i] = new Button(comp, SWT.PUSH | SWT.FLAT);
				buttons[i].setText(actions[i].label);
				buttons[i].setBackground(parent.getDisplay().getSystemColor(
						SWT.COLOR_LIST_SELECTION));
				buttons[i].setForeground(parent.getDisplay().getSystemColor(
						SWT.COLOR_LIST_SELECTION_TEXT));
				buttons[i].addListener(SWT.Selection, actions[i].listener);
				buttons[i].pack();
			}
			final Control content = createContent(comp);
			final Control bottomLeft = createBottomLeft(comp);
			// layout
			comp.setLayout(new FormLayout());
			FormAttachment bottom = new FormAttachment(buttons[0]);
			if (bottomLeft != null) {
				bottomLeft.pack();
				if (bottomLeft.getSize().y > buttons[0].getSize().y)
					bottom = new FormAttachment(bottomLeft);
				FormData fd = new FormData();
				fd.bottom = new FormAttachment(100);
				fd.left = new FormAttachment(0);
				bottomLeft.setLayoutData(fd);
			}
			FormData fd = new FormData();
			fd.top = new FormAttachment(0);
			fd.left = new FormAttachment(0);
			fd.right = new FormAttachment(100);
			fd.bottom = bottom;
			content.setLayoutData(fd);
			for (int i = 0; i < buttons.length; i++) {
				fd = new FormData();
				if (i == buttons.length - 1)
					fd.right = new FormAttachment(100);
				else
					fd.right = new FormAttachment(buttons[i + 1]);
				fd.bottom = new FormAttachment(100);
				buttons[i].setLayoutData(fd);
			}
			return comp;
		}
	}

	protected Action[] getActions() {
		return null;
	}

	/**
	 * Crée le contenu de la page
	 * 
	 * @param parent
	 * @return
	 */
	protected abstract Control createContent(Composite parent);

	/**
	 * Crée le contrôle en bas à droite. Appelé uniquement s'il existe des
	 * actions.
	 * 
	 * @param parent
	 * @return le contrôle ou <code>null</code> si aucun
	 */
	protected Control createBottomLeft(Composite parent) {
		return null;
	}

	protected static class Action {
		public String label;
		public Listener listener;

		public Action(String label, Listener listener) {
			this.label = label;
			this.listener = listener;
		}
	}

}
